#include "Event.h"

string Event::sendStatusEvent(STATUS_TYPE_EVENT status) const {
	switch (status){
		case DONE:
			return "Done";
			break;
		case NOT_DONE:
			return "Not Done";
			break;
	}
}
Event::Event(void): _statusCompleted(false)
{
	_entryType=TYPE_ENTRY;
}
Event::Event(string name, QDate endDate, QTime endTime, QDate startDate, QTime startTime, bool statusCompleted)
	: Entry(name, endDate, endTime), _startDate(startDate), _startTime(startTime), _statusCompleted(statusCompleted) {
		_entryType=TYPE_ENTRY;
}
Event::Event(const Entry* other): Entry(other->getName(), other->getEndDate(), other->getEndTime()), _startDate(other->getStartDate()), _startTime(other->getStartTime()), _statusCompleted(other->getStatus()) {
	_entryType=TYPE_ENTRY;
}

Event::~Event(void)
{
}

bool Event::isEmpty() {
	return (Entry::isEmpty());
}
QDate Event::getStartDate() const {
	return _startDate;
}
QTime Event::getStartTime() const {
	return _startTime;
}
bool Event::isConflict(Entry* newEvent) {
	if(getEndDate().isValid() && getEndTime().isValid() && getStartDate().isValid() && getStartTime().isValid() &&
		newEvent->getEndDate().isValid() && newEvent->getEndTime().isValid() && newEvent->getStartDate().isValid() && newEvent->getStartTime().isValid()){
		int endToNewEnd = QDateTime(getEndDate(), getEndTime()).secsTo(QDateTime(newEvent->getEndDate(), newEvent->getEndTime()));
		int newStartToNewEnd =QDateTime(newEvent->getStartDate(),newEvent->getStartTime()).secsTo(QDateTime(newEvent->getEndDate(), newEvent->getEndTime()));
		if(endToNewEnd>0) {
			if(endToNewEnd<newStartToNewEnd) {
				return true;
			} else {
				return false;
			}
		}

		int newEndToEnd = QDateTime(newEvent->getEndDate(), newEvent->getEndTime()).secsTo(QDateTime(getEndDate(), getEndTime()));
		int startToEnd =QDateTime(getStartDate(),getStartTime()).secsTo(QDateTime(getEndDate(), getEndTime()));
		if(endToNewEnd<0) {
			if(newEndToEnd < startToEnd) {
				return true;
			} else {
				return false;
			}
		}
		if(endToNewEnd ==0) {
			return true;
		}
		return false;
		}
	return false;
}
string Event::toString() {
	char buffer[1000];
	sprintf_s(buffer,MSG_OUTPUT.c_str(), 
		TYPE_ENTRY.c_str(), getName().c_str(), 
		getEndDate().toString(FORMAT_DATE).toStdString().c_str(),getEndTime().toString(FORMAT_TIME).toStdString().c_str(), 
		getStartDate().toString(FORMAT_DATE).toStdString().c_str(),getStartTime().toString(FORMAT_TIME).toStdString().c_str(), _statusCompleted);
	return buffer;
}
Event Event::fromString(string input) {
	istringstream inStream(input);
	string name;
	string endDate;
	string endTime;
	string startDate;
	string startTime;
	string dump;
	bool statusCompleted;
	getline(inStream, dump, ' ');
	getline(inStream, name);
	getline(inStream, dump, ' ');
	getline(inStream, endDate);
	getline(inStream, dump, ' ');
	getline(inStream, endTime);
	getline(inStream, dump, ' ');
	getline(inStream, startDate);
	getline(inStream, dump, ' ');
	getline(inStream, startTime);
	getline(inStream, dump, ' ');
	inStream >> statusCompleted;
	return Event(name, QDate::fromString(QString::fromStdString(endDate), FORMAT_DATE), QTime::fromString(QString::fromStdString(endTime), FORMAT_TIME),
		QDate::fromString(QString::fromStdString(startDate),FORMAT_DATE), QTime::fromString(QString::fromStdString(startTime),FORMAT_TIME), statusCompleted);
}
bool Event::operator==(Entry* input) {
	return (getName()==input->getName() &&
		getEndDate()==input->getEndDate() &&
		getEndTime()==input->getEndTime() &&
		getStartDate()==input->getStartDate() &&
		getStartTime()==input->getStartTime() &&
		getEntryType()==input->getEntryType() &&
		getStatus()==input->getStatus());
}
void Event::setStartDate(QDate startDate) {
	_startDate=startDate;
}
void Event::setStartTime(QTime startTime) {
	_startTime=startTime;
}
string Event::getEntryType() const {
	return _entryType;
}
//void Event::setName(string name) {
//	Entry::setName(name);
//}
string Event::getSearchString() const {
	char buffer[1000];
	sprintf_s(buffer ,"%s %s %s %s %s %s", getName().c_str(), getEndDate().toString("dddd MMMM yyyy dd/MM/yy dd/MM/yyyy dd/M/yy dd/M/yyyy").toStdString().c_str(), getEndTime().toString("h:mm hmmap H:mm Hmm").toStdString().c_str(), getStartDate().toString("dddd MMMM yyyy dd/MM/yy dd/MM/yyyy dd/M/yy dd/M/yyyy").toStdString().c_str(), getStartTime().toString("h:mm hmmap H:mm Hmm").toStdString().c_str(), getStatusString().c_str());
	return buffer;
}
int Event::getAttributeCount() const {
	return Event::NUM_ATTRIBUTES;
}

bool Event::setDone() {
	if(_statusCompleted) {
		_statusCompleted = false;
	} else {
		_statusCompleted = true;
	}
	return _statusCompleted;
}

bool Event::getStatus() const {
	return _statusCompleted;
}
string Event::getStatusString() const {
	//return (_statusCompleted==true ? sendStatusTask(DONE) : sendStatusTask(NOT_DONE));
	//return (_statusCompleted==true? "Done" : "Not Done");
	if(_statusCompleted){
		return sendStatusEvent(DONE);
	}
	else{
		return sendStatusEvent(NOT_DONE);
	}
}

bool Event::isOngoingOrNot(QDateTime target) const {
	//only if startdatetime and enddatetime is valid.
	
	//QDate const currentDate = QDate::currentDate();
	//QTime const currentTime = QTime::currentTime();
	QDate const startDate = getStartDate();
	QTime const startTime = getStartTime();
	QDate const endDate = getEndDate();
	QTime const endTime = getEndTime();
	/*
	int yyyyStart = startDate.year();
	int mmStart = startDate.month();
	int ddStart = startDate.day();
	int yyyyEnd = endDate.year();
	int mmEnd = endDate.month();
	int ddEnd = endDate.day();

	int hourStart = startTime.hour();
	int minsStart = startTime.minute();
	int secStart = startTime.second();
	int hourEnd = endTime.hour();
	int minsEnd = endTime.minute();
	int secEnd = endTime.second();
	*/
	if(!(startDate.isValid() && startTime.isValid() && endDate.isValid() && endTime.isValid())){
		return false;
	}
	//if((QDate::isValid(yyyyStart, mmStart, ddStart))|| (QTime::isValid(hourStart, minsStart, secStart)) || (QDate::isValid(yyyyEnd, mmEnd, ddEnd)) || (QTime::isValid(hourEnd, minsEnd, secEnd)))
		//return false;
	QDateTime const eventStartDateTime (startDate, startTime); 
	//QDateTime const eventCurrentDateTime (currentDate, currentTime);
	QDateTime const eventEndDateTime(endDate, endTime); 
	return ((eventEndDateTime > target)&&(eventStartDateTime < target));
	//return(((eventEndDateTime > eventCurrentDateTime)&&(eventStartDateTime < eventCurrentDateTime)) ? 1 : 0);
}

const string Event::MSG_OUTPUT= "%s\nNAME: %s\nDUE_DATE: %s\nDUE_TIME: %s\nSTART_DATE: %s\nSTART_TIME: %s\nSTATUS: %d\n";
const string Event::TYPE_ENTRY= "EVENT";